import numpy as np


def euclidean_distance(x1, x2):
    return np.sqrt(np.sum((x1 - x2) ** 2))


def knn_predict(X_train, y_train, x_test, k=3):
    distances = [euclidean_distance(x_test, x) for x in X_train]
    indices = np.argsort(distances)[:k]
    k_nearest_labels = [y_train[i] for i in indices]

    # 进行投票，选择票数最多的类别
    unique_labels, counts = np.unique(k_nearest_labels, return_counts=True)
    majority_label = unique_labels[np.argmax(counts)]

    return majority_label


# 示例用法
X_train = np.array([[1, 2], [2, 3], [3, 4]])
y_train = np.array([0, 1, 0])
x_test = np.array([2.5, 3.5])

prediction = knn_predict(X_train, y_train, x_test)
print("Predicted Label:", prediction)
